import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/controller/schooladmin.dart';
import 'package:ichinesereader/routes/routePages.dart';
import 'package:ichinesereader/themes/pageStyles.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/buttons/floatingActionStudentManagement.dart';
import 'package:ichinesereader/widgets/layouts/appdrawerSchoolAdmin.dart';
import 'package:ichinesereader/widgets/grids/studentsDatagrid.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';
import 'package:ichinesereader/widgets/sidebars/adminSidebar.dart';

// ignore: must_be_immutable
class AdminStudentsManagement extends StatefulWidget {
  AdminStudentsManagement({
    Key? key,
    this.classId,
  }) : super(key: key);
  String? classId;
  static const String routeName = '/schooladmin/students';

  @override
  _AdminStudentsManagementState createState() =>
      _AdminStudentsManagementState();
}

class _AdminStudentsManagementState extends State<AdminStudentsManagement> {
  final box = Hive.box('appconfig');
  final SchoolAdminController _adminController = Get.find();
  bool isSchoolAdmin = false;
  String? classId;

  @override
  void initState() {
    classId = widget.classId;
    isSchoolAdmin = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_SchoolAdmin;

    if (isSchoolAdmin) {
      if (classId != null && classId != '') {
        Future.delayed(Duration(milliseconds: 200), () {
          _adminController.fetchSchoolStudentsByClassId(
              classId.toString(), false);
        });
      } else {
        String currentClassId =
            box.get(CacheManagerKey['currentclass'].toString()) ?? 'All';
        if (currentClassId != 'All') {
          Future.delayed(Duration(milliseconds: 200), () {
            _adminController.fetchSchoolStudentsByClassId(
                currentClassId, false);
          });
        } else {
          Future.delayed(Duration(milliseconds: 100), () {
            _adminController.fetchSchoolAllStudents(0, 250);
          });
        }
      }
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build AdminStudentsManagement');
    final isSmallScreen =
        MediaQuery.of(context).size.width < Constants.DefinedScreenSmallSize;
    final isBigScreen =
        MediaQuery.of(context).size.width > Constants.DefinedScreenBigSize;
    int _selectedSidebarItem = RoutePages.schoolAdminPageItems
        .indexWhere((page) => page.route == Get.currentRoute);
    final PageStyles _pageStyles = PageStyles();

    return Scaffold(
      appBar:
          isSmallScreen ? CustomAppBar(title: 'Student Management'.tr) : null,
      drawer: (classId == null || classId == '') && isSmallScreen
          ? SchoolAdminAppDrawer()
          : null,
      floatingActionButton: FloatingActionButtonStudentManagement(),
      body: Container(
        decoration: _pageStyles.pageBoxDecoration(),
        child: Row(
          children: [
            Visibility(
                visible: !isSmallScreen,
                child: AdminSidebarX(
                    selectedIndex: _selectedSidebarItem,
                    isBigScreen: isBigScreen)),
            Expanded(
              child: Padding(
                padding: EdgeInsets.only(top: 33.0, right: 12.0),
                child: Center(
                  child: Visibility(
                      visible: isSchoolAdmin,
                      child: _adminController.obx(
                        (data) => StudentsDataGrids(true,
                            _adminController.studentItems, classId == null),
                        onEmpty: StudentsDataGrids(true,
                            _adminController.studentItems, classId == null),
                        onLoading: Container(),
                      )),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
